package com.freesky.web.manager.controller;

import com.freesky.common.annotation.SystemLogAnno;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.pojo.UserLoginInfo;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.web.manager.service.ILoginService;
import com.freesky.web.manager.util.VerifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 15:25 2019-01-10
 * @description:
 */
@Controller
@Api(description = "登录页面")
@Slf4j
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    private HttpServletRequest request;

    @GetMapping("/login")
    @ApiOperation(value = "进入页面-登录页")
    public String loginPage() {
        return "login";
    }

    @ApiOperation(value = "登录获取随机验证码")
    @GetMapping("/login/get-code")
    @ResponseBody
    public String getVerifyCode() {
        String verifyCode = VerifyUtil.getRandomCode();
        //将验证码放入session中
        request.getSession().setAttribute("verifyCode", verifyCode);
        return verifyCode;
    }

    @ApiOperation(value = "登录验证")
    @PostMapping("/login/validate")
    @ResponseBody
    @SystemLogAnno(description="用户登录")
    public JSONResponse validateLogin(
            @RequestParam(value = "username") String username,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "verifyCode") String verifyCode) {
        return  loginService.validateLogin(username,password,verifyCode,request);
    }

    @GetMapping(value = "/logout")
    @ApiOperation(value = "退出登录")
    public String logout() {
        return loginService.logout();
    }


}
